package com.jzx.provider.ucenter.client;

import com.jzx.provider.ucenter.UcenterService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 用户信息参数解析
 *
 * @author
 */
@AllArgsConstructor
@Slf4j
public class UserInfoArgumentResolver implements HandlerMethodArgumentResolver {

    private UcenterService userService;

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(CurrentUserInfo.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                                  ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) {

        WebRequest webRequest = nativeWebRequest;
        String token = webRequest.getHeader("token");
        String platform = webRequest.getHeader("platform");
        log.info("token", token);
        return userService.findUserBytoken(token, platform);
    }
}
